var h = 0;

function button(ref,value){
ref.value=value;
}

function start(){


var len = document.getElementById('len').value;
var alg = document.getElementById('alg').value;
var repeats = document.getElementById('repeats').value;
var rernd = document.getElementById('rernd').value;
var increase = document.getElementById('increase').value;
var decimal = document.getElementById('decimal').value;
document.write('Algoithm Comparison by Jan Zimmermann');
document.write('<LINK href="css.css" rel="stylesheet" type="text/css"> ')
document.write('<div class="datagrid">')
if(increase == 'yes'){

	document.write('Started Bubble Sort with: '+repeats + ' repeats and '+len+' as a max length when increasing.<p></p>');
	var results = [];
	var array = [];
document.write('<div id="content"><table>');
document.write('<tr><td>N</td>');

for(var g=1; g<=rernd;g++){
document.write('<td>Trial '+g+'</td>');
}
document.write('<td>Average</td>');
document.write('<td>Max</td>');
document.write('<td>Min</td>');
document.write('</tr>');

for(var l=1; l <= len; l++){
document.write('<tr><td>'+l+'</td>');

var totalsum = 0;

for(var g=1; g<=rernd;g++){
	
	for(var i=0; i < repeats; i++){
		var rndarray = [];
		rndarray=createarray(l);
		//results[i] = bubbleSort(rndarray);
		
		results[i] = sort(rndarray, alg,len,repeats);
		//document.write('<p>Steps of '+ i + ' : ' + results[i] +' Array Was ' + rndarray + ' Final Array Was '+bubbleSortra(rndarray)+'</p>');
		//document.write('*')
		}
		
	var sum = 0;
	for(var i=0; i<repeats; i++){
	sum=sum+results[i];
	}
	var average = sum/repeats;
	//document.write('<p>Length: ' + l + ' Average: '+average);
	//document.write('<p>'+ l + ','+average);
	
	document.write('<td>'+average.toFixed(decimal)+'</td>');
	totalsum = totalsum + average;

	}
	var max = l*l;
	var min = 2*l;
	var totalaverage = totalsum/rernd;
	document.write('<td>'+totalaverage.toFixed(decimal)+'</td>');
	document.write('<td>'+max+'</td>');
	document.write('<td>'+min+'</td>');
	document.write('</tr>');
	}
	
	document.write('</table></div>');

}



if(increase == 'no'){

	document.write('Started Bubble Sort with: '+repeats + ' repeats and '+len+' as a length<p></p>');
	var results = [];
	var array = [];
	

	for(var i=0; i < repeats; i++){
	var rndarray = [];
	rndarray=createarray(len);
	var disp = rndarray;
	document.write('New Array:'+disp);
	//results[i] = bubbleSort(rndarray);
	results[i] = sort(rndarray, alg,len,repeats);
	document.write('<p>Steps of '+ i + ' : ' + results[i].toFixed(decimal) +'</p>');
	
	}
	
	
	
	var sum = 0;
	for(var i=0; i<repeats; i++){
	sum=sum+results[i];
	}
	var average = sum/repeats;
	document.write('<p>Average:'+average.toFixed(decimal)+'</p>');
	document.write('Done!');
	}
document.write('</div>')
}


function createarray(len){
//document.write(' *Created Array* ')
for (var arr=[],i=0;i<len;++i) arr[i]=i;

var shuffled=shuffle(arr);
//document.write('SHUFFLED:'+shuffled);
return shuffled;
}

function shuffle(o){ //v1.0
//document.write(' *Shuffled Array* ')
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
}

function bubbleSort(a)
{
//document.write('//STARTEDBUBBLE:'+a+'//');
    var swapped;
	var t=0;
	var s=0;
    do {
        swapped = false;
        for (var i=0; i < a.length-1; i++) {
            if (a[i] > a[i+1]) {
                var temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                swapped = true;
				t=t+1;
				s=s+1;
            }else{
				t=t+1;
			}
        }
    } while (swapped);
	//document.write('//DIDBUBBLE:'+t+'//');
	return t;
	
}



function bubbleSortra(a)
{
    var swapped;
	var t=0;
	var s=0;
    do {
        swapped = false;
        for (var i=0; i < a.length-1; i++) {
            if (a[i] > a[i+1]) {
                var temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                swapped = true;
				t=t+1;
				s=s+1;
            }else{
				t=t+1;
			}
        }
    } while (swapped);
	return a;
}

function swap(items, firstIndex, secondIndex){
    var temp = items[firstIndex];
    items[firstIndex] = items[secondIndex];
    items[secondIndex] = temp;
}

function selectionSort(items){

    var len = items.length,
        min;

	var t =0;
    for (i=0; i < len; i++){

        //set minimum to this position
        min = i;

        //check the rest of the array to see if anything is smaller
        for (j=i+1; j < len; j++){
            if (items[j] < items[min]){
                min = j;
            }
	t=t+1;
        }

        //if the minimum isn't in the position, swap it
        if (i != min){
            swap(items, i, min);
		
        }
    }

    return t;
}

function insertionSort(items) {
	var t =0;
    var len     = items.length,     // number of items in the array
        value,                      // the value currently being compared
        i,                          // index into unsorted section
        j;                          // index into sorted section
    
    for (i=0; i < len; i++) {
    
        // store the current value because it may shift later
        value = items[i];
        
        /*
         * Whenever the value in the sorted section is greater than the value
         * in the unsorted section, shift all items in the sorted section over
         * by one. This creates space in which to insert the value.
         */
        for (j=i-1; j > -1 && items[j] > value; j--) {
            items[j+1] = items[j];
	t=t+1;
        }

        items[j+1] = value;
    }
    
    return t;
}


function sort(set,alg,len,repeats){

if(alg == "bubble"){
return bubbleSort(set);
}

if(alg == "selection"){
return selectionSort(set);
}
if(alg == "insertion"){
return insertionSort(set);
}


h=h+1;
if(alg == "comp"){
	if(h < (len*repeats)) {
		return bubbleSort(set);
	}else if(h < (2*(len*repeats))){
		return selectionSort(set);
	}else if(h < (3*(len*repeats))){
		return insertionSort(set);
	}else{
	return false;

	}
}


return false;



}
